package bean;

import java.io.Serializable;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Hashtable;

import rmi.ServeurRmiInterface;

import annotation.Form;
import annotation.Req;

public class User implements Serializable{

	static int port = 54000;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Form (type="text", label="Login")
	@Req (mess="Veuillez saisir un login")
	private String login = "";
	
	@Form (type="password", label="password")
	@Req (mess="Veuillez saisir un mot de passe")
	private String password = "";
	
	private String prenom ="";
	
	private String adresse = "";
	
	private int codePostal;
	
	private String ville = "";
	
	private int telephone;
	
	private boolean isConnected = true;
	
	Hashtable <String,String> err = new Hashtable<String, String>();
	
	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public String getPrenom() {
		return prenom;
	}
	
	public Hashtable<String, String> getErr() {
		return err;
	}

	public void setErr(Hashtable<String, String> err) {
		this.err = err;
	}

	public void setConnected(boolean isConnected) {
		this.isConnected = isConnected;
	}

	public boolean isConnected() {
		return isConnected;
	}

	public String getAdresse() {
		return adresse;
	}

	public void setAdresse(String adresse) {
		this.adresse = adresse;
	}

	public int getCodePostal() {
		return codePostal;
	}

	public void setCodePostal(int codePostal) {
		this.codePostal = codePostal;
	}

	public String getVille() {
		return ville;
	}

	public void setVille(String ville) {
		this.ville = ville;
	}

	public int getTelephone() {
		return telephone;
	}

	public void setTelephone(int telephone) {
		this.telephone = telephone;
	}
	
	public static User getUser(String login) {
		try {
			Registry registry = LocateRegistry.getRegistry(port);
			ServeurRmiInterface serveur = (ServeurRmiInterface) registry.lookup("ServeurRMI");
			return serveur.getUser(login);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static boolean modifierUser(User user) {
		try {
			Registry registry = LocateRegistry.getRegistry(port);
			ServeurRmiInterface serveur = (ServeurRmiInterface) registry.lookup("ServeurRMI");
			return serveur.modifier_user(user);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
